<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #wrap{
            width: 600px;
            height: 300px;
            border: 2px solid rgba(87, 86, 86, 0.3);
            border-radius: 15px;
            box-shadow: 10px 10px 20px 10px rgba(0, 0, 0, 0.2);

            position: absolute;
            left: 0;
            right: 0;
            top: 70px;
            margin: auto;

            display: flex;
            flex-direction: column;
            align-items: center;
            padding-top: 40px;
        }

        #wrap input,#wrap textarea{
            width: 75%;
            height: 40px;
            margin-bottom: 10px;
            border-radius: 15px;
            border: 2px solid rgba(48, 47, 47, 0.3);
        }

        #wrap textarea{
            height: 150px;
            padding: 5px;
        }

        #wrap input[type = "submit"]{
            width: 30%;
            height: 40px;
            padding: 5px;
            margin: 10px;
            box-sizing: border-box;
            border: 2px solid rgba(245, 134, 134, 0.82);
            background: #e91e63;
            color: #fff;
        }

        #wrap input[type = "reset"]{
            width: 30%;
            height: 40px;
            padding: 5px;
            margin: 10px;
            box-sizing: border-box;
            border: 2px solid rgba(245, 134, 134, 0.82);
            font-size: 16px;
            background: #e91e63;
            color: #fff;
        }
    </style>
</head>
<body>
    <h2 style="text-align:center;">新增博客</h2>
    <!-- 标题 内容 作者(x) 创建时间(x) -->
    <form id="wrap" action="">
        <input id="title" type="text" placeholder="输入标题">
        <textarea id="content" style="resize: none;" placeholder="输入内容"></textarea>
        <div style="width: 75%;display: flex;justify-content: space-between;">
            <input type="reset">
            <input type="submit" id="submit">
        </div>
    </form>
</body>
<script>
    window.onload=function(){
        var submit = document.querySelector("#submit");
        submit.addEventListener("click",function(event){
            event.preventDefault();
            //获取标题 内容
            var title = document.querySelector("#title").value;
            var content = document.querySelector("#content").value;
            var token = localStorage.getItem("token");
            //发送ajax请求 参数:标题 内容 token
            var xhr = new XMLHttpRequest();
            xhr.addEventListener("load", function () {
                //请求完成
                var res = JSON.parse(this.responseText)
                if(res.data === "error"){
                    //登录操作失败 提醒一下用户
                    alert(res.message)
                }else{
                    window.location.href = "./blog.html";
                }
            });
            xhr.open("POST", "http://127.0.0.1:8080/api/blog/new");
            xhr.withCredentials=true;
            xhr.send(JSON.stringify({
                title:title,
                content:content,
                token:token
            }));
        })
    }
</script>
</html>